{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\86188\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\sklearn\\feature_extraction\\text.py:17: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working\n",
      "  from collections import Mapping, defaultdict\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import fetch_mldata\n",
    "mnist = fetch_mldata('MNIST original', data_home='./')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'DESCR': 'mldata.org dataset: mnist-original',\n",
       " 'COL_NAMES': ['label', 'data'],\n",
       " 'target': array([0., 0., 0., ..., 9., 9., 9.]),\n",
       " 'data': array([[0, 0, 0, ..., 0, 0, 0],\n",
       "        [0, 0, 0, ..., 0, 0, 0],\n",
       "        [0, 0, 0, ..., 0, 0, 0],\n",
       "        ...,\n",
       "        [0, 0, 0, ..., 0, 0, 0],\n",
       "        [0, 0, 0, ..., 0, 0, 0],\n",
       "        [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mnist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X,y = mnist['data'],mnist['target']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = X[:60000],X[60000:],y[:60000],y[60000:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([47929, 19232, 11238, ..., 42981,  8995, 17664])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "shuffle_index = np.random.permutation(60000)\n",
    "shuffle_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,y_train = X[shuffle_index],y[shuffle_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAANpElEQVR4nO3db6xU9Z3H8c9XLQ+UJoB3NCAutzZg1piUkgnZxE1jbbZRicE+qMIDZJMmtw/EQMSkpE2shiekrjY1MU3oQnpduzaYlgUj2a3BJoQHVkcDgiVFivyrN9wBEnv7gHSx3z64x+YCc37nMufMnIHv+5VMZuZ858z5Zrgfzsz5zZmfubsAXPuuq7sBAP1B2IEgCDsQBGEHgiDsQBA39HNjQ0NDPjw83M9NAqEcO3ZMZ86csU61UmE3s/sl/UTS9ZL+0903pR4/PDysVqtVZpMAEprNZm6t67fxZna9pJckPSDpLkkrzeyubp8PQG+V+cy+VNIRdz/q7n+V9EtJy6tpC0DVyoT9Nkknp9w/lS27iJmNmFnLzFrtdrvE5gCUUSbsnQ4CXPbdW3ff7O5Nd282Go0SmwNQRpmwn5J0+5T78yV9Uq4dAL1SJuzvSlpoZl8ysxmSVkjaWU1bAKrW9dCbu18wszWS/k+TQ29b3f3DyjoDUKlS4+zuvkvSrop6AdBDfF0WCIKwA0EQdiAIwg4EQdiBIAg7EARhB4Ig7EAQhB0IgrADQRB2IAjCDgRB2IEgCDsQBGEHgiDsQBCEHQiCsANBEHYgCMIOBEHYgSAIOxAEYQeCIOxAEIQdCIKwA0EQdiAIwg4EQdiBIErN4gr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v3epLb7V+ZgfQP3Xv2QH0CWEHgqgl7GZ2v5n9wcyOmNmGOnrIY2bHzOxANg11q+ZetprZuJkdnLJsjpm9aWYfZdcd59irqbeBmMY7Mc14ra9d3dOf9/0zu5ldL+mwpH+TdErSu5JWuvvv+9pIDjM7Jqnp7rV/AcPMvibpL5Jedve7s2U/knTO3Tdl/1HOdvfvDUhvz0j6S93TeGezFc2dOs24pIcl/btqfO0SfT2iPrxudezZl0o64u5H3f2vkn4paXkNfQw8d98j6dwli5dLGs1uj2ryj6XvcnobCO4+5u7vZ7cnJH0+zXitr12ir76oI+y3STo55f4pDdZ87y7pN2b2npmN1N1MB7e6+5g0+ccj6Zaa+7lU4TTe/XTJNOMD89p1M/15WXWEvdNUUoM0/nePuy+R9ICkx7O3q5ieaU3j3S8dphkfCN1Of15WHWE/Jen2KffnS/qkhj46cvdPsutxSds1eFNRn/58Bt3serzmfv5hkKbx7jTNuAbgtatz+vM6wv6upIVm9iUzmyFphaSdNfRxGTO7KTtwIjO7SdI3NXhTUe+UtDq7vVrSjhp7ucigTOOdN824an7tap/+3N37fpH0oCaPyP9R0g/q6CGnrzsk7c8uH9bdm6RXNfm27v81+Y7oO5JulrRb0kfZ9ZwB6u2/JB2Q9IEmgzW3pt7+VZMfDT+QtC+7PFj3a5foqy+vG1+XBYLgG3RAEIQdCIKwA0EQdiAIwg4EQdiBIAg7EMTfAa5yOtysgto/AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "some_digit = X[36000]\n",
    "some_digit_image = some_digit.reshape(28,28)\n",
    "plt.imshow(some_digit_image,cmap = matplotlib.cm.binary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用KN分类器预测MINST训练集\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn_clf = KNeighborsClassifier(n_neighbors = 5)\n",
    "knn_clf.fit(X_train, y_train)\n",
    "knn_clf.predict([some_digit])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_predict\n",
    "y_train_pred = cross_val_predict(knn_clf,X_train,y_train,cv=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 使用交叉验证评估KN的准确率\n",
    "# cross_val_score(knn_clf, X_train, y_train, cv=3, scoring=\"accuracy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5887,    4,    4,    1,    0,    4,   15,    2,    2,    4],\n",
       "       [   1, 6708,   11,    0,    2,    0,    2,   12,    1,    5],\n",
       "       [  41,   70, 5705,   15,    6,    4,    8,   92,   11,    6],\n",
       "       [   5,   17,   41, 5922,    0,   57,    6,   33,   28,   22],\n",
       "       [   5,   57,    2,    1, 5638,    0,   16,   13,    2,  108],\n",
       "       [  18,   12,    5,   69,    7, 5208,   62,    8,    7,   25],\n",
       "       [  24,   11,    2,    0,    8,   29, 5843,    0,    1,    0],\n",
       "       [   2,   64,   16,    2,   15,    1,    0, 6101,    2,   62],\n",
       "       [  20,   79,   23,   85,   28,  106,   26,   17, 5407,   60],\n",
       "       [  17,   14,    9,   50,   60,   10,    3,   77,   12, 5697]],\n",
       "      dtype=int64)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获得混淆矩阵\n",
    "from sklearn.metrics import confusion_matrix\n",
    "matr = confusion_matrix(y_train,y_train_pred)\n",
    "confusion_matrix(y_train,y_train_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPoAAAECCAYAAADXWsr9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAKdElEQVR4nO3dz4td9RnH8c9nZhRNrOjQQjCTdBIoaUQokaFEE1wkLtoqCqGLFCLUzWxajSKIduM/IKKLIgyxbgy6iFk0UqwFdVECoZNJID/GARPND00wJVTFTZzM08W9QpJJc890znfOPXneLxAy15OvD+O8c869Ofd7HRECcHMbaHoAAOUROpAAoQMJEDqQAKEDCRA6kEBjodv+le0Z25/afqGpOaqyvcr2R7anbR+zvbPpmaqwPWj7kO33mp6lCtt32d5j+5Pu9/qBpmfqxfaz3Z+Jo7bftn1b0zNdq5HQbQ9K+rOkX0u6V9LvbN/bxCwLMCvpuYhYL2mjpD+0YGZJ2ilpuukhFuA1Se9HxM8l/UJ9PrvtlZKeljQWEfdJGpS0vdmp5mvqjP5LSZ9GxMmIuCTpHUmPNzRLJRFxLiKmur/+Vp0fwJXNTnVjtkckPSJpV9OzVGH7TkkPSXpDkiLiUkT8p9mpKhmSdLvtIUnLJH3Z8DzzNBX6Sklnrvj6rPo8mivZHpW0QdKBZifp6VVJz0uaa3qQitZKuiDpze7TjV22lzc91I1ExBeSXpZ0WtI5SV9HxAfNTjVfU6H7Oo+14l5c23dIelfSMxHxTdPz/C+2H5X0VUQcbHqWBRiSdL+k1yNig6TvJPX16ze271bnanSNpHskLbe9o9mp5msq9LOSVl3x9Yj68HLnWrZvUSfy3RGxt+l5etgk6THbn6vz1GiL7beaHamns5LORsQPV0p71Am/nz0s6bOIuBAR30vaK+nBhmeap6nQ/yXpZ7bX2L5VnRcv/trQL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pu8ACiRE6kAChAwkQOpAAoQMJEDqQAKEDCVQK3fazto/ZPmr7bdu3lR4MQH16hm57paSnJY1FxH2SBiVtLz0YgPpUvXQfknS77SFJyyR9WW4kAHXrGXpEfCHpZUmnJZ2T9HVEfFB6MAD1qXLpfrekxyWtkXSPpOW2d1znuHHbk7Yn6x8TwGJUuXR/WNJnEXEhIr6XtFfSg9ceFBETETEWEfXfqQ9gUaqEflrSRtvLbFvSVknTZccCUKcqz9EPSNojaUrSke7vmSg8F4AaDVU5KCJekvRS4VkAFMKdcUAChA4kQOhAAoQOJEDoQAKEDiTQqu2e26hzj1G71i61jXRJAwNlzlklvxczMzO1r7lt2zYdPXqU7Z6BjAgdSIDQgQQIHUiA0IEECB1IgNCBBAgdSIDQgQQIHUiA0IEECB1IgNCBBAgdSIDQgQQIHUiA0IEECB1IgNCBBAgdSIDQgQRK7QJ7QdKpiof/WNK/ax+inLbNKzHzUuiXeX8aET+59sEioS+E7cmIGGt0iAVo27wSMy+Ffp+XS3cgAUIHEuiH0CeaHmCB2javxMxLoa/nbfw5OoDy+uGMDqAwQgcSIHQgAUIHEiB0IIH/AkSzfVbuB832AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 288x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 使用matplotlib的matshow 函数来查看混淆矩阵的图像表示\n",
    "plt.matshow(matr, cmap = plt.cm.gray)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.969092369342432"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import precision_score,recall_score\n",
    "\n",
    "precision_score(y_train,y_train_pred,average=\"macro\")  # 训练集精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9681019923757688"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recall_score(y_train,y_train_pred,average=\"macro\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_pred=knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9692753386570571"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "precision_score(y_test,test_pred,average=\"macro\") # 测试集精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 974,    1,    1,    0,    0,    1,    2,    1,    0,    0],\n",
       "       [   0, 1133,    2,    0,    0,    0,    0,    0,    0,    0],\n",
       "       [  11,    8,  991,    2,    1,    0,    1,   15,    3,    0],\n",
       "       [   0,    3,    3,  976,    1,   13,    1,    6,    3,    4],\n",
       "       [   3,    7,    0,    0,  944,    0,    4,    2,    1,   21],\n",
       "       [   5,    0,    0,   12,    2,  862,    4,    1,    2,    4],\n",
       "       [   5,    3,    0,    0,    3,    2,  945,    0,    0,    0],\n",
       "       [   0,   22,    4,    0,    3,    0,    0,  988,    0,   11],\n",
       "       [   8,    3,    5,   13,    6,   12,    5,    5,  913,    4],\n",
       "       [   5,    7,    3,    9,    7,    3,    1,   10,    2,  962]],\n",
       "      dtype=int64)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_matr = confusion_matrix(y_test,test_pred)\n",
    "confusion_matrix(y_test,test_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
